home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / term43-source.lha / Extras / Source / gtlayout-Source.lha / LTP_Rescale.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-21  |  2.3 KB  |  112 lines

  1. /*  GadTools layout toolkit
  2. **
  3. **  Copyright © 1993-1994 by Olaf `Olsen' Barthel
  4. **  Freely distributable.
  5. */
  6.  
  7. #include "gtlayout_global.h"
  8.  
  9. BOOLEAN __regargs
  10. LTP_Rescale(LayoutHandle *handle,BOOLEAN trimWidth,BOOLEAN trimHeight)
  11. {
  12.     STATIC struct TextAttr topazAttr =
  13.     {
  14.         "topaz.font",
  15.         8,
  16.         FS_NORMAL,
  17.         FPF_ROMFONT | FPF_DESIGNED
  18.     };
  19.  
  20.     LONG    oldWidth;
  21.     LONG    oldHeight;
  22.     UBYTE    glyph;
  23.     LONG    glyphWidth;
  24.     LONG    count;
  25.     LONG    i;
  26.     LONG    total;
  27.     BOOLEAN found;
  28.  
  29.     oldWidth    = handle -> GlyphWidth;
  30.     oldHeight    = handle -> RPort . TxHeight;
  31.  
  32.     if(handle -> CloseFont)
  33.     {
  34.         CloseFont(handle -> RPort . Font);
  35.  
  36.         handle -> CloseFont = FALSE;
  37.     }
  38.  
  39.     /* GfxBase -> DefaultFont is guaranteed not to ever be removed from
  40.      * memory,so we use that fact...
  41.      */
  42.  
  43.     LTP_SetFont(handle,GfxBase -> DefaultFont);
  44.  
  45.     handle -> TextAttr            = &handle -> CopyTextAttr;
  46.     handle -> CopyTextAttr . tta_Name    = handle -> RPort . Font -> tf_Message . mn_Node . ln_Name;
  47.     handle -> CopyTextAttr . tta_YSize    = handle -> RPort . TxHeight;
  48.     handle -> CopyTextAttr . tta_Style    = handle -> RPort . Font -> tf_Style;
  49.     handle -> CopyTextAttr . tta_Flags    = handle -> RPort . TxFlags;
  50.  
  51.     if(handle -> RPort . Font -> tf_Style & FSF_TAGGED)
  52.         handle -> CopyTextAttr . tta_Tags = ((struct TextFontExtension *)handle -> RPort . Font -> tf_Extension) -> tfe_Tags;
  53.  
  54.     FOREVER
  55.     {
  56.         count = 0;
  57.         total = 0;
  58.  
  59.         for(i = 32 ; i < 127 ; i++)
  60.         {
  61.             glyph = i;
  62.             total += TextLength(&handle -> RPort,&glyph,1);
  63.             count++;
  64.         }
  65.  
  66.         for(i = 160 ; i < 256 ; i++)
  67.         {
  68.             glyph = i;
  69.             total += TextLength(&handle -> RPort,&glyph,1);
  70.             count++;
  71.         }
  72.  
  73.         glyphWidth = total / count;
  74.  
  75.         found = TRUE;
  76.  
  77.         if(trimWidth && (glyphWidth >= oldWidth))
  78.             found = FALSE;
  79.  
  80.         if(trimHeight && (oldHeight >= handle -> RPort . TxHeight))
  81.             found = FALSE;
  82.  
  83.         if(found)
  84.         {
  85.             handle -> GlyphWidth    = glyphWidth;
  86.             handle -> Rescaled    = TRUE;
  87.  
  88.             if(glyphWidth <= 8)
  89.                 handle -> InterWidth = 4;
  90.             else
  91.                 handle -> InterWidth = glyphWidth / 2;
  92.  
  93.             if(handle -> RPort . TxHeight <= 8)
  94.                 handle -> InterHeight = 2;
  95.             else
  96.                 handle -> InterHeight = handle -> RPort . TxHeight / 4;
  97.  
  98.             LTP_ResetGroups(handle -> TopGroup);
  99.  
  100.             return(TRUE);
  101.         }
  102.  
  103.         if(handle -> TextAttr == &topazAttr)
  104.             return(FALSE);
  105.  
  106.         LTP_SetFont(handle,OpenFont(&topazAttr));
  107.  
  108.         handle -> TextAttr    = &topazAttr;
  109.         handle -> CloseFont    = TRUE;
  110.     }
  111. }
  112.